if(files.cwd().endsWith("脚本")){
  let _require = require;
  require = function(path){
    if(path.startsWith("./")){
      path = path.replace("./","");
    }
    path = files.cwd() + "/gdt_hb/" + path;
    return _require(path);
  }
}


auto.waitFor()
var utils = require("./utils.js");
var storage = storages.create("xiangxiangyuedu");
var time = random(2000, 3000)

function launchAppEnhance(app_name) {
  let thread = threads.start(function () {
    toastLog("检查是否有打开权限!");
    while (true) {
      var allow = text("允许").findOne(5000);
      if (allow) {
        sleep(1000)
        toastLog("发现允许授权!");
        allow.click();
        sleep(1000);
        thread.interrupt();
        break;
      }
    }
  });
  sleep(1000);
  var result = launchApp(app_name);
  sleep(3000);
  thread.interrupt();
  return result;
}

function 广告() {
  let try_count = 13, close_btn = null;
  while (!close_btn) {
    if (try_count <= 0) {
      break
    }
    // 直到找到关闭按钮
    close_btn = idContains("ad_close").findOne(1000) ||
      className('android.widget.ImageView').clickable().depth(5).drawingOrder(2).findOne(1000)
    if (close_btn) {
      close_btn.click()
      sleep(time)
      break
    } else {
      try {
        取消 = text("取消").findOne(500)
        if (取消) {
          点击控件(取消)
        }
      } catch (error) { }
      try {
        关闭 = text("关闭").findOne(500)
        if (关闭) {
          点击控件(关闭)
        }
      } catch (error) { }
      try {//支付宝
        if (currentPackage() != 'com.mz.wlm') {
          toastLog('估计是进入支付宝页面了')
          sleep(2000)
          back()
          sleep(2000)
          继续 = textStartsWith("继续").findOne(1000)
          if (继续) {
            点击控件(继续)
          }
          var 每日任务 = text('每日任务').findOne(3000)
          if (每日任务) {
            break
          }
        }
      } catch (error) { }
    }
    try_count--
    sleep(2000)
  }
}

function login() {
  var 我的 = text('我的').findOne(20000)
  if (!我的) {
    if (device.model == 'Redmi 8A') {
      click(random(80, 650), random(799, 887))
    }
    if (device.model == 'MI 5X') {
      var pwd = id('com.mz.wlm:id/pwdEt').findOne(3000)
      if (pwd) {
        pwd.click()
        sleep(time)
        click(random(200, 900), random(883, 998))
      }
    }
    sleep(time)
    var 登录 = text('登录').findOne(5000)
    if (登录) {
      登录.click()
    }
  } else {
    toastLog('已经在登录状态')
  }
}

function task() {
  var 我的 = text('我的').findOne(5000)
  if (我的) {
    我的.parent().click()
    sleep(time)
    var 每日任务 = text('每日任务').findOne(5000)
    if (每日任务) {
      每日任务.click()
      sleep(time)
      var 已签 = text('已签').findOne(5000)
      if (已签) {
        toastLog('已经完成任务')
      } else {
        var index = 1
        while (index < 10) {
          var 去完成 = text('去完成').findOne(5000)
          if (去完成) {
            var finalActivity = currentActivity();
            去完成.click()
            sleep(10000)
            if (currentActivity() != 'com.mz.wlm.activity.me.SignInActivity') {
              toastLog('当前是广告页面')
              utils.waitAdvert(finalActivity);
              var 每日任务 = text('每日任务').findOne(3000)
              if (!每日任务) {
                toastLog('新增关闭按钮')
                sleep(time)
                back()
              }
            }
            sleep(5000)
          } else {
            toastLog('已经完成任务')
            sleep(time)
            index = 20
          }
          index++
          if(index*5 < 100){
            utils.updateAppProgress(index*5);
          }
        }
        var 签到 = text('签到').findOne(5000)
        if (签到) {
          签到.click()
        }
      }
    }
  }
}

function main() {
  var open_app = launchAppEnhance("玩乐喵")
  if (open_app) {
    var thread_1 = threads.start(function () {
      toastLog("检查广告弹窗")
      while (true) {
        var 去看看 = text('去看看').findOne(2000)
        if (去看看) {
          去看看.click();
          sleep(time)
          if (currentActivity() != 'com.mz.wlm.activity.MainActivity') {
            back()
          }
        }
      }
    });
    login()
    sleep(10000)
    task()
    thread_1.interrupt();
  }
  else {
    toastLog("玩乐喵打开失败，请确认是否安装并授权打开!");
  }
}

try {
  main();
  utils.updateAppProgress(100);
} catch (error) {
  toastLog(error);
}

// 点击控件坐标，控件的clickbale为false的时候
function 点击控件(x) {
  var weight = x.bounds()
  click(weight.centerX(), weight.centerY())
}

storage.put("script_running_status", "end");
